package com.cavalier.surtr.amop.provider;

import com.cavalier.surtr.config.MyBatisConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by archangle on 17/3/6.
 */
@Component
public class MessageSender implements RabbitTemplate.ConfirmCallback {

    private static Logger logger = LoggerFactory.getLogger(MessageSender.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String exchangeName,String routeKey,String jsonStr){
        rabbitTemplate.convertAndSend(exchangeName,routeKey,jsonStr);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        String callBackId=correlationData.getId();
        logger.info("回调id："+callBackId);
        if(ack){
            logger.info("回调成功，客户端正常消费");
        }else{
            logger.error("消息消费失败:" + cause);
        }
    }
}
